/**
 * Copyright (c) 2010 sonntagsfarben.de
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
 
package de.sonntagsfarben.tools 
{
	import flash.geom.Rectangle;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	/**
	 * @author Soenke Kluth <sonntagsfarben.de>
	 * @copyright (c) 2009 Soenke Kluth.
	 */
	public class PositionTool 
	{
		private var _mouseDownPoint : Point;
		private var _targetStartPoint : Point;
		private var _target : DisplayObject;
		private var _stage : Stage;
		private var _roundPosition : Boolean;
		private static const _tools : Dictionary = new Dictionary();
		private static var _currentTarget : DisplayObject;

		public function PositionTool(target : DisplayObject, roundPosition : Boolean = true) 
		{
			_target = target;
			_roundPosition = roundPosition;
			if(_target.stage)
				_init();
			else
				_target.addEventListener(Event.ADDED_TO_STAGE, _init);
		}

		
		public static function add(target : DisplayObject, roundPosition : Boolean = true) : PositionTool
		{
			if(_tools[target])
			{
				return _tools[target];
			}
			
			_tools[target] = new PositionTool(target, roundPosition);
			
			return _tools[target];
		} 

		
		public static function remove(target : DisplayObject) : void
		{
			if(_tools[target])
			{
				(_tools[target] as PositionTool)._stage.removeEventListener(KeyboardEvent.KEY_DOWN, (_tools[target] as PositionTool).onKeyDown);
				(_tools[target] as PositionTool)._stage.removeEventListener(MouseEvent.MOUSE_DOWN, (_tools[target] as PositionTool).onMouseDown);
				if(_currentTarget == target)
					_currentTarget = null;
				(_tools[target] as PositionTool)._target = null;
				delete(_tools[target]);
			}
		}

		
		private function _init(e : Event = null) : void 
		{
			_stage = _target.stage;
			
			if(_roundPosition)
			{
				_target.x = int(_target.x);
				_target.y = int(_target.y);
			}
			
			_stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}

		
		private function _logPosition() : void 
		{
			var target : DisplayObject = (_currentTarget) ? _currentTarget : _target;
			trace("DEBUG: " + this + " :: target: " + target + "   y: " + target.y + "  x: " + target.x + "  scaleX: " + target.scaleX + "  scale: " + target.scaleY);
		}

		
		/*
		 * cursors for x,y
		 * y,x,s for scaling. hold shift for scale down
		 */
		private function onKeyDown(event : KeyboardEvent) : void
		{
			
			var target : DisplayObject = (_currentTarget) ? _currentTarget : _target;
			
			switch(event.keyCode) {
				case 32:
					_logPosition();
					if(_currentTarget)
					{
						event.stopImmediatePropagation();
					}
					break;
				case 37:
					target.x--;
					break;
				case 38:
					target.y--;
					break;
				case 39:
					target.x++;
					break;
				case 40:
					target.y++;
					break;
				case 88:
				case 83:
					if(event.shiftKey)
						target.scaleX -= .01;
					else
						target.scaleX += .01;
				case 89:
				case 83:
					if(event.shiftKey)
						target.scaleY -= .01;
					else
						target.scaleY += .01;
					break;
				default:
			}
		}

		
		private function onMouseMove(event : MouseEvent) : void
		{
			_target.x = _targetStartPoint.x + (event.stageX - _mouseDownPoint.x);
			_target.y = _targetStartPoint.y + (event.stageY - _mouseDownPoint.y);
		}

		
		private function onMouseUp(event : MouseEvent) : void 
		{
			_stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			_stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}

		
		private function onMouseDown(event : MouseEvent) : void 
		{
			var bounds : Rectangle = _target.getBounds(_stage);
			if(bounds.containsPoint(new Point(event.stageX, event.stageY)))
			{
				_currentTarget = _target;
				_mouseDownPoint = new Point(event.stageX, event.stageY);
				_targetStartPoint = new Point(_target.x, _target.y);
				_stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp, false, 10, true);
				_stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove, false, 10, true);
			}
		}

		
		public function get target() : DisplayObject
		{
			return _target;
		}
	}
}